﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextMIS
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
        }
        string str_state = "Update";
        sysopr SysInfo = new sysopr();
        dbopr UserDB = new dbopr();
        //xtBiz xtbiz = new xtBiz();

        private void frmUser_Load(object sender, EventArgs e)
        {
            DG_User.DataSource = UserDB.Get_userlist(); //获取用户列表

            cb_WorkGroup.DataSource = UserDB.Get_workgroup();//用户组
            cb_WorkGroup.DisplayMember = "wg_name";
            cb_WorkGroup.ValueMember = "wg_id";

            cb_UserType.DataSource = SysInfo.GetDicInfo(13);//人员属性
            cb_UserType.DisplayMember = "xmmc";
            cb_UserType.ValueMember = "bzdm";

            cb_Bran.DataSource = UserDB.Get_Bran_list();//科室
            cb_Bran.DisplayMember = "Bra_name";
            cb_Bran.ValueMember = "Bra_id";
        }
        private void DG_User_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            DataGridViewRow dgr = DG_User.SelectedRows[0];
            string str_Userid = dgr.Cells["User_Id"].Value.ToString().Trim();

            DataTable dt = UserDB.Get_userinfo(str_Userid);
            txt_UserId.Text = dt.Rows[0]["User_id"].ToString().Trim();
            txt_UserName.Text = dt.Rows[0]["User_Name"].ToString().Trim();
            txt_UserCode.Text = dt.Rows[0]["User_Code"].ToString().Trim();
            txt_Password.Text = dt.Rows[0]["User_Pwd"].ToString().Trim();
            //cb_rysx.SelectedValue = dt.Rows[0]["rysx"].ToString().Trim();
            cb_UserType.Text = dt.Rows[0]["User_Type"].ToString().Trim();
            cb_WorkGroup.SelectedValue = dt.Rows[0]["Workgroup_Id"].ToString().Trim();
            cb_Bran.SelectedValue = dt.Rows[0]["Bran_Id"].ToString().Trim();

            string str_enable = dt.Rows[0]["IsEnable"].ToString().Trim();
            if (str_enable == "1") ckb_IsEnable.Checked = true; else ckb_IsEnable.Checked = false;

            str_state = "Update";
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void ClearControl()
        {
            txt_UserId.Text = "";
            txt_UserName.Text = "";
            txt_UserCode.Text = "";
            txt_Password.Text = "";
            cb_UserType.SelectedIndex = -1;
            cb_WorkGroup.SelectedIndex = -1;
            cb_Bran.SelectedIndex = -1;
            ckb_IsEnable.Checked = false;
        }
        private void bt_add_Click(object sender, EventArgs e)
        {
            ClearControl();
            str_state = "Insert";
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            if (txt_UserName.Text.Trim() == "")
            {
                MessageBox.Show("请填写操作员名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = txt_UserName;
                return;
            }
            if (txt_Password.Text.Trim() == "")
            {
                MessageBox.Show("请填写密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = txt_Password;
                return;
            }
            if (object.Equals(null, cb_WorkGroup.SelectedValue))
            {
                MessageBox.Show("请选择用户组！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_WorkGroup;
                return;
            }
            if (str_state == "Insert" && UserDB.Exist_UserCode(txt_UserCode.Text.Trim()) > 0)
            {
                MessageBox.Show("该操作员编码已被使用，请修改！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = txt_UserCode;
                return;
            }
            if (txt_UserId.Text.Trim() == "")
            {
                txt_UserId.Text = UserDB.Get_MaxStr("UserInfo", "User_Id");
            }
            string str_Workgrp = "";
            if (!object.Equals(null, cb_WorkGroup.SelectedValue)) str_Workgrp = cb_WorkGroup.SelectedValue.ToString().Trim();
            string str_barn = "";
            if (!object.Equals(null, cb_Bran.SelectedValue)) str_barn = cb_Bran.SelectedValue.ToString().Trim();

            string str_workgroup = "";
            if (!object.Equals(null, cb_WorkGroup.SelectedValue)) str_workgroup = cb_WorkGroup.SelectedValue.ToString().Trim();

            string str_usertype = cb_UserType.Text.Trim();
            string str_enable = "0";
            if (ckb_IsEnable.Checked) str_enable = "1";
            UserDB.Insert_Userinfo(txt_UserId.Text.Trim(), txt_UserName.Text.Trim(), txt_Password.Text.Trim(), str_workgroup , str_usertype, str_enable);
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DG_User.DataSource = UserDB.Get_userlist();
                       
            str_state = "Update";
        }

        private void txt_czymc_Leave(object sender, EventArgs e)
        {
            //txt_UserCode.Text = xtbiz.Get_Py(txt_UserName.Text.ToString().Trim()).ToLower();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (txt_UserId.Text.Trim() == "") return;
            //非管理员权限不能删除用户-  后期加
            //if (Program.czylx != "系统" && Program.czylx != "管理")
            //{
            //    MessageBox.Show("您不是系统管理员，无权删除操作员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}

            //检查是否引用-记录表是否有相关医生信息
            //if (SysInfo.Exists_xt_czy(txt_UserId.Text.Trim()) > 0)
            //{
            //    MessageBox.Show("该操作员被引用了，不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            UserDB.Delete_User(txt_UserId.Text.Trim());
            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ClearControl();
            DG_User.DataSource = UserDB.Get_userlist();
            str_state = "Update";
        }

    }
}
